﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WPFSample.CutCopyAndPasteSample
{
    class CutCopyAndPaste : Window
    {
        private TextBlock text;
        protected MenuItem itemCut, itemCopy, itemPaste, itemDelete;

        public CutCopyAndPaste()
        {
            this.Title = "Cut,Copy,and Paste";

            DockPanel dock = new DockPanel();
            this.Content = dock;

            Menu menu = new Menu();
            dock.Children.Add(menu);
            DockPanel.SetDock(menu, Dock.Top);

            this.text = new TextBlock()
            {
                Text = "Sample Clipboard text",
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                FontSize = 32,
                TextWrapping = TextWrapping.Wrap
            };
            dock.Children.Add(this.text);

            MenuItem itemEdit = new MenuItem();
            itemEdit.Header = "_Edit";
            itemEdit.SubmenuOpened += this.EditOnOpened;
            menu.Items.Add(itemEdit);

            this.itemCut = new MenuItem();
            this.itemCut.Header = "Cu_t";
            this.itemCut.Click += this.CutOnClick;
            itemEdit.Items.Add(this.itemCut);

            this.itemCopy = new MenuItem();
            this.itemCopy.Header = "_Copy";
            this.itemCopy.Click += this.CopyOnClick;
            itemEdit.Items.Add(this.itemCopy);

            this.itemPaste = new MenuItem();
            this.itemPaste.Header = "_Paste";
            this.itemPaste.Click += this.PasteOnClick;
            itemEdit.Items.Add(this.itemPaste);

            this.itemDelete = new MenuItem();
            this.itemDelete.Header = "_Delete";
            this.itemDelete.Click += this.DeleteOnClick;
            itemEdit.Items.Add(this.itemDelete);
        }

        protected void DeleteOnClick(object sender, RoutedEventArgs e)
        {
            this.text.Text = null;
        }

        protected void PasteOnClick(object sender, RoutedEventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                this.text.Text = Clipboard.GetText();
            }
        }

        protected void CopyOnClick(object sender, RoutedEventArgs e)
        {
            if (this.text.Text != null && this.text.Text.Length > 0)
            {
                Clipboard.SetText(this.text.Text);
            }
        }

        protected void CutOnClick(object sender, RoutedEventArgs e)
        {
            this.CopyOnClick(sender, e);
            this.DeleteOnClick(sender, e);
        }

        private void EditOnOpened(object sender, RoutedEventArgs e)
        {
            this.itemCut.IsEnabled =
                this.itemCopy.IsEnabled =
                this.itemDelete.IsEnabled = this.text.Text != null && this.text.Text.Length > 0;
            this.itemPaste.IsEnabled = Clipboard.ContainsText();
        }
    }
}
